(function (window) {
	function Character(imgChar,x,y) {
		this.initialize(imgChar,x,y);
	}

	Character.prototype = new BitmapAnimation();

	//What wire is Character on (not sure if gonna be used)
	Character.prototype.curWire = 0;
	// 1 = right/up, -1 = left/down
	Character.prototype.direction = 1;

	Character.prototype.path = [];
	Character.prototype.xVels = [];
	Character.prototype.yVels = [];
	Character.prototype.totalWires = 0;

	Character.prototype.BitmapAnimation_initialize = Character.prototype.initialize;

	Character.prototype.initialize = function(imgChar,x, y) {
		var localSpriteSheet = new SpriteSheet ({
			images: [imgChar],
			frames: {width: 64, height: 64, regX: 32, regY: 32},
			animations: {
				bounce: [0, 11, "bounce",2]
			}
		});

		SpriteSheetUtils.addFlippedFrames(localSpriteSheet,true,false,false);

		this.BitmapAnimation_initialize(localSpriteSheet);
		this.previousBottom = 0.0;
		this.elapsed = 0;
		this.gotoAndPlay("bounce");
		this.vX = 0.0;
		this.vY = 0.0;
		this.x  = x;
		this.y = y;
	}

	Character.prototype.Dispose = function() {
		Character.prototype.path = [];
		Character.prototype.vX = 0;
		Character.prototype.vY = 0;
		Character.prototype.curWire = 0;
		Character.prototype.xVels = [];
		Character.prototype.yVels = [];
	}

	Character.prototype.Update = function() {
		this.vX = this.xVels[this.curWire];
		this.vY = this.yVels[this.curWire];


		if(this.vX === undefined)
			return;
		if(this.vY === undefined)
			return;
		this.x += this.vX;
		this.y += this.vY;

		if(this.vX > 0) {
			if(this.x >= this.path[this.curWire])
			{
				this.curWire++;
				this.vX = this.xVels[this.curWire];
				this.vY = this.yVels[this.curWire];
			}
		} else if(this.vY != 0) {
			if(this.vY < 0) {
				if(this.y <= this.path[this.curWire]) {
					this.curWire++;
					this.vX = this.xVels[this.curWire];
					this.vY = this.yVels[this.curWire];
				}
			} else if(this.vY > 0) {
				if(this.y >= this.path[this.curWire]) {
					this.curWire++;
					this.vX = this.xVels[this.curWire];
					this.vY = this.yVels[this.curWire];
				}
			}
		}
		if(this.curWire >= this.totalWires) {
			var day = new Date();
			var time = day.getTime();
			Map.prototype.timeElapsed = (time - Map.prototype.timeStarted)/1000;
			this.vX = undefined;
			this.vY = undefined;
			this.curWire = 0;
			this.path = [];
			this.xVels = [];
			this.yVels = [];
			$("#menuTray").css("display","none");
			$("#levelPanel").css("display","block");
			$("#levelScore").css("display","none");
			$("#lvlBack").css("display","none");
			$("#lvlOptions").css("display","none");
			$("#formula").css("display","block");
			$("#results").css("display","block");
                var cvs = document.createElement("canvas");
                cvs.width = 400;
                cvs.height = 100;
                var current = Map.prototype.current;
                var ctx = cvs.getContext("2d");
                ctx.font = '30px "Fredoka One"';
                ctx.fillStyle = "white";
                ctx.strokeStyle="black";
                ctx.fillText("I = V/R  V = " + Map.prototype.voltage + " R = " + Math.round(Map.prototype.resistance*100)/100,0,64);
                ctx.strokeText("I = V/R  V = " + Map.prototype.voltage+" R = " + Math.round(Map.prototype.resistance*100)/100,0,64);
                ctx.fillText("I = " + Math.round(current*100)/100, 85,96);
                ctx.strokeText("I = " + Math.round(current*100)/100, 85,96);
                $("#formula img").attr("src",cvs.toDataURL());
			var store = JSON.parse(localStorage.getItem("world" + window.curWorld));
			console.log("time: "  + Map.prototype.timeElapsed);
			    $("#resultStar1").removeClass("star");
                $("#resultStar2").removeClass("star");
                $("#resultStar3").removeClass("star");
			if(current >= Map.prototype.max) {
			    $("#resultStar1").addClass("star");
                $("#resultStar2").addClass("star");
                $("#resultStar3").addClass("star");
                store.stars[window.curLvl] = 3;
            } else if(Map.prototype.currents.length > 1 && current >= Map.prototype.currents[1]) {
            	$("#resultStar1").addClass("star");
                $("#resultStar2").addClass("star");
                if(store.stars[window.curLvl] < 2) 
                	store.stars[window.curLvl] = 2;
            } else {
       			$("#resultStar1").addClass("star");
                if(store.stars[window.curLvl] < 1)
                	store.stars[window.curLvl] = 1;
            }
            if(curLvl != 8) {
            	if(store.stars[window.curLvl+1] == null)
            		store.stars[window.curLvl+1] = 0;
            }
            localStorage.setItem("world" + window.curWorld,JSON.stringify(store));

		}

    }

Character.prototype.moveCharacter = function (x,y) {
    this.x = x;
    this.y = y;
}

window.Character = Character;
}(window));